package com.alliky.core.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;

import com.alliky.core.api.BaseApi;
import com.alliky.core.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.io.File;

/**
 * 图片加载工具类
 */
public class ImageLoader {

    private static float radius = 4f;
    private static int errorRes = R.mipmap.icon_default;

    private static String getImageUrl(String url) {
        if (!TextUtils.isEmpty(url) && url.startsWith("http")) {
            return url;
        } else {
            return BaseApi.BASE_IMAGE + url;
        }
    }

    public static void loadImage(Context context, String url, ImageView imageView, @DrawableRes int errorRes, float radius) {
        Glide.with(context)
                .load(getImageUrl(url))
                .placeholder(errorRes)
                .error(errorRes)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideRoundTransform(radius))
                .into(imageView);
    }

    public static void loadImage(Context context, File file, ImageView imageView, @DrawableRes int errorRes, float radius) {
        Glide.with(context)
                .load(file)
                .placeholder(errorRes)
                .error(errorRes)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideRoundTransform(radius))
                .into(imageView);
    }

    public static void loadImage(Context context, Bitmap bitmap, ImageView imageView, @DrawableRes int errorRes, float radius) {
        Glide.with(context)
                .load(bitmap)
                .placeholder(errorRes)
                .error(errorRes)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideRoundTransform(radius))
                .into(imageView);
    }

    public static void loadImage(Context context, Drawable drawable, ImageView imageView, @DrawableRes int errorRes, float radius) {
        Glide.with(context)
                .load(drawable)
                .placeholder(errorRes)
                .error(errorRes)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideRoundTransform(radius))
                .into(imageView);
    }

    public static void loadImage(Context context, Uri uri, ImageView imageView, @DrawableRes int errorRes, float radius) {
        Glide.with(context)
                .load(uri)
                .placeholder(errorRes)
                .error(errorRes)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideRoundTransform(radius))
                .into(imageView);
    }

    public static void loadImage(Context context, String url, ImageView imageView, @DrawableRes int errorRes) {
        loadImage(context, url, imageView, errorRes, radius);
    }

    public static void loadImage(Context context, File file, ImageView imageView, @DrawableRes int errorRes) {
        loadImage(context, file, imageView, errorRes, radius);
    }

    public static void loadImage(Context context, Bitmap bitmap, ImageView imageView, @DrawableRes int errorRes) {
        loadImage(context, bitmap, imageView, errorRes, radius);
    }

    public static void loadImage(Context context, Drawable drawable, ImageView imageView, @DrawableRes int errorRes) {
        loadImage(context, drawable, imageView, errorRes, radius);
    }

    public static void loadImage(Context context, Uri uri, ImageView imageView, @DrawableRes int errorRes) {
        loadImage(context, uri, imageView, errorRes, radius);
    }

    public static void loadImage(Context context, String url, ImageView imageView, float radius) {
        loadImage(context, url, imageView, errorRes, radius);
    }

    public static void loadImage(Context context, File file, ImageView imageView, float radius) {
        loadImage(context, file, imageView, errorRes, radius);
    }


    public static void loadImage(Context context, Bitmap bitmap, ImageView imageView, float radius) {
        loadImage(context, bitmap, imageView, errorRes, radius);
    }


    public static void loadImage(Context context, Drawable drawable, ImageView imageView, float radius) {
        loadImage(context, drawable, imageView, errorRes, radius);
    }


    public static void loadImage(Context context, Uri uri, ImageView imageView, float radius) {
        loadImage(context, uri, imageView, errorRes, radius);
    }

    public static void loadImage(Context context, String url, ImageView imageView) {
        loadImage(context, url, imageView, errorRes, radius);
    }

    public static void loadImage(Context context, File file, ImageView imageView) {
        loadImage(context, file, imageView, errorRes, radius);
    }


    public static void loadImage(Context context, Bitmap bitmap, ImageView imageView) {
        loadImage(context, bitmap, imageView, errorRes, radius);
    }


    public static void loadImage(Context context, Drawable drawable, ImageView imageView) {
        loadImage(context, drawable, imageView, errorRes, radius);
    }


    public static void loadImage(Context context, Uri uri, ImageView imageView) {
        loadImage(context, uri, imageView, errorRes, radius);
    }


}
